//
//  ShareViewController.swift
//  SwiftPanda
//
//  Created by 余意 on 2017/2/28.
//  Copyright © 2017年 余意. All rights reserved.
//

import UIKit
import Alamofire
import SwiftyJSON
import HandyJSON

class ShareViewController:  UIViewController ,UITableViewDelegate,UITableViewDataSource  {
    
    var shareTableView : UITableView!
    var modelArray : Array<ShareModel> = []

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = UIColor.white
        
        self.shareTableView = UITableView.init(frame:self.view.bounds,style:.plain)
        self.shareTableView.delegate = self
        self.shareTableView.dataSource = self
        //        self.shareTableView.separatorStyle = .none
        self.shareTableView.rowHeight = 90
        self.shareTableView.register(ShareCell.classForCoder(), forCellReuseIdentifier: "cell")
        self.view.addSubview(self.shareTableView)
        
        let urlString = "http://cms.99bx.cn/apis/panda/getRecentPosts?type=3&page=1&counts=100"
        
        NetworkTools.requestData(.get, URLString: urlString) { (result) in
            if let models = JSONDeserializer<ShareModelS>.deserializeFrom(json: result.description)
            {
                self.modelArray = models.results!
                self.shareTableView.reloadData()
            }
        }
        
        
//        Alamofire.request("http://cms.99bx.cn/apis/panda/getRecentPosts?type=3&page=1&counts=100", method:.get).responseJSON{ response in
//            debugPrint(response)
//            
//            if let value = response.result.value
//            {
//                let json = JSON(value).description
//                
//                if let models = JSONDeserializer<ShareModelS>.deserializeFrom(json: json )
//                {
//                    self.modelArray = models.results!
//                    self.shareTableView.reloadData()
//                }
//            }
//        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.modelArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell : ShareCell!
        cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! ShareCell
        let model : ShareModel = self.modelArray[indexPath.row]
        cell.model = model
        cell.selectionStyle = .none
        cell.accessoryType = .disclosureIndicator
        return cell
    }

}
